package com.sk.sourcecircle.activity.login;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.app.MyApplication;
import com.sk.sourcecircle.base.BaseFragment;
import com.sk.sourcecircle.callback.HandleDataCallBack;
import com.sk.sourcecircle.utils.AppApi;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.view.VerificationCodeButton;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by sk on 2017/11/28.
 */

public class RegisterFragment extends BaseFragment implements HandleDataCallBack {
    @BindView(R.id.ed_account)
    EditText edAccount;
    @BindView(R.id.ed_code)
    EditText edCode;
    @BindView(R.id.get_code)
    VerificationCodeButton getCode;
    @BindView(R.id.ed_password)
    EditText edPassword;
    @BindView(R.id.to_visiable)
    ImageView toVisiable;
    @BindView(R.id.to_invisiable)
    ImageView toInvisiable;
    @BindView(R.id.register)
    TextView register;
    @BindView(R.id.ed_name)
    EditText edName;
    Unbinder unbinder;

    private LoadingDialog loadingDialog;


    @Override
    protected int getContentView() {
        return R.layout.fragment_register;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
    }

    @OnClick({R.id.get_code, R.id.to_visiable, R.id.to_invisiable, R.id.register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.get_code:
                if (!TextUtils.isEmpty(edAccount.getText().toString()) && edAccount.getText().toString().startsWith("1") && edAccount.getText().toString().length() == 11) {
                    loadingDialog.show();
                    getCode.startTimer();
                    AppApi.sendCode(edAccount.getText().toString(), 19, RegisterFragment.this, 1, getActivity());
                } else {
                    Toast.makeText(mContext, "请填写您的手机号码！", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.to_visiable:
                edPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                toVisiable.setVisibility(View.GONE);
                toInvisiable.setVisibility(View.VISIBLE);
                break;
            case R.id.to_invisiable:
                edPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                toVisiable.setVisibility(View.VISIBLE);
                toInvisiable.setVisibility(View.GONE);
                break;
            case R.id.register:
                List<EditText> editTexts = new ArrayList<>();
                edName.setTag("请输入您的昵称！");
                edAccount.setTag("请输入您的手机号码！");
                edPassword.setTag("请输入您的密码！");
                edCode.setTag("请输入您的验证码！");
                editTexts.add(edName);
                editTexts.add(edAccount);
                editTexts.add(edPassword);
                editTexts.add(edCode);
                if (MyUtils.isFormat(mRootView, editTexts)) {
                    loadingDialog.show();
                    AppApi.register(edAccount.getText().toString(), edPassword.getText().toString(),edName.getText().toString(), edCode.getText().toString(), MyApplication.getInstance().getLat(), MyApplication.getInstance().getLng(), RegisterFragment.this, 2, getActivity());
                }
                break;
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        switch (tag) {
            case 1:
                Toast.makeText(mContext, "验证码发送成功！", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(mContext, "账号注册成功！", Toast.LENGTH_SHORT).show();
                if (getActivity() instanceof LoginActivity) {
                    ((LoginActivity) getActivity()).getMyPager().setCurrentItem(0);
                }
                break;
        }
    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
